package com.uchengwang.ui.cm_ui.rychat.msgtype;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.RyGroupBean;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

import static com.uchengwang.R.id.tv_type;

/**
 * Created by U城网 on 2017/10/20.
 * 发送U城自定义信息
 */

@ProviderTag(
        messageContent = UcTypeMessage.class,
        showProgress = false,
        showReadState = true
)
public class UcItemProvider extends IContainerItemProvider.MessageProvider<UcTypeMessage>  {

    private static final String TAG = "UcItemProvider";

    public UcItemProvider() {
    }

    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_ry_uc_type_item, (ViewGroup)null);
        UcItemProvider.ViewHolder holder = new UcItemProvider.ViewHolder();
        holder.tv_ln = (LinearLayout) view.findViewById(R.id.tv_ln);
        holder.tv_type = (TextView) view.findViewById(tv_type);
        holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
        holder.tv_cover = (ImageView) view.findViewById(R.id.tv_cover);
        view.setTag(holder);
        return view;
    }

    /**
     * type //1-U学，2-U赛，3-U行，4-U用，5-U公益活动倡议，6-U公益失物招领,7-社团活动, 8-社团分享)  9校吧帖子
     * 10社团纳新[当为社团纳新时, ID为社团账号]  11 享说动态 12推荐群  13推荐社团活动季给好友
     * */

    public void onItemClick(final View view, int position, UcTypeMessage content, final UIMessage message) {
        if (content != null){
            int type = content.getUcType();
            long id = content.getUcId();
            Intent intent = null;
            switch (type){
                case BaseConfig.U_STUDY:
                case BaseConfig.U_GAME:
                case BaseConfig.U_WALK:
                case BaseConfig.U_USE:
                case BaseConfig.U_BOOK:
//                    intent = new Intent(view.getContext(), UCity3Activity.class);
//                    intent.putExtra(BaseConfig.PUB_TYPE, type);
//                    intent.putExtra(BaseConfig.ID, id);
                    break;
                case BaseConfig.U_LOST:
                    break;
                case BaseConfig.U_BENEFIT:
                case BaseConfig.U_CLUB_SHARE:
                case BaseConfig.U_SCHOOL_ARTICLE:
                case BaseConfig.U_HOT_ARTICLE:
//                    intent = new Intent(view.getContext(), SchoolNewDetailActivity.class);
//                    intent.putExtra(BaseConfig.ID, id);

                    break;
                case BaseConfig.CLUB_NEW_MEMBER://社团纳新
//                    intent = new Intent(view.getContext(), ClubAccessDetailActivity.class);
//                    intent.putExtra(BaseConfig.ORG_ID, id);
                    break;
                case BaseConfig.SPEEK_TYPE://享说动态
//                    intent = new Intent(view.getContext(), SpeekDetailActivity.class);
//                    intent.putExtra(BaseConfig.ID, id);
                    break;
                case BaseConfig.GROUP_REMEND://群推荐
                    LoadingUtils.getUtils().loading(view.getContext());
                    RyGroupManager.getManager().getGroupInfo((Activity) view.getContext(), id, new OnHttpResultSingleListener<RyGroupBean>() {
                        @Override
                        public void onResult(int status, String msg, RyGroupBean ryGroupBean) {
                            LoadingUtils.getUtils().hide();

                            if (status != 0 || ryGroupBean == null){
                                UCommonUtil.getUtil().showToast(view.getContext(), "获取信息失败");
                            }else {
                                if (message.getMessageDirection() == Message.MessageDirection.RECEIVE){
                                    //只有自己收到的才可以点击申请加入
                                    DialogUtils.getUtils().inviteGroup(view.getContext(), ryGroupBean, false, null);
                                }else {
                                    //跳转到聊天界面
                                    UCommonUtil.getUtil().toChatActiviy(view.getContext(),ryGroupBean.getId(), Conversation.ConversationType.GROUP, ryGroupBean.getGroupName());
                                }
                            }
                        }
                    });
                    break;
                case BaseConfig.CLUB_ACT:
//                    intent = new Intent(view.getContext(), ClubActShareActivity.class);
                    break;
            }
            if (intent != null){
                view.getContext().startActivity(intent);
            }

        }

    }

    public void bindView(View v, int position, UcTypeMessage content, UIMessage message) {
        if (content == null){
            return;
        }

        UcItemProvider.ViewHolder holder = (UcItemProvider.ViewHolder)v.getTag();
        if(message.getMessageDirection() == Message.MessageDirection.SEND) {//自己发送的
            holder.tv_ln.setBackgroundResource(R.drawable.ucity_image);
        } else {
            holder.tv_ln.setBackgroundResource(R.drawable.rc_ic_bubble_left);
        }

        holder.tv_type.setText(getType(content.getUcType()));
//        holder.tv_type.setText(content.getUcTitle());

        holder.tv_title.setText(content.getUcTitle());
        ImageManager.getInstance().loadImage(v.getContext(), holder.tv_cover, content.getUcCover(), ImageManager.ImageType.ROUND);
    }

    public Spannable getContentSummary(UcTypeMessage data) {
        return new SpannableString("["+getType(data.getUcType())+"]");
    }

    private String getType(int type){
        String string = "[暂无该消息类型]";
        switch (type){
            //1-U学，2-U赛，3-U行，4-U用，5-U公益活动倡议，6-U公益失物招领,7-社团活动, 8-社团分享)  9校吧帖子
            case 1:
                string = "U学";
                break;
            case 2:
                string = "U赛";
                break;
            case 3:
                string = "U行";
                break;
            case 4:
                string = "U用";
                break;
            case 5:
                string = "公益活动";
                break;
            case 6:
                string = "失物招领";
                break;
            case 7:
                string = "社团活动";
                break;
            case 8:
                string = "社团分享";
                break;
            case 9:
                string = "校吧热帖";
                break;
            case 10:
                string = "社团纳新";
                break;
            case 11:
                string = "享说动态";
                break;
            case 12:
                string = "群推荐";
                break;
            case 13:
                string = "社团活动季";
                break;
        }
        return string;
    }

    private static class ViewHolder {
        private LinearLayout tv_ln;
        private TextView tv_type, tv_title;
        private ImageView tv_cover;
    }
}
